package com.guoqing.redis.practice;

import com.alibaba.fastjson.JSON;
import com.guoqing.redis.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author guoQ.z
 * @date 2021/1/4 13:40
 * TODO
 */
@Service
public class RedisHashService {
    private final String HASH_KEY = "HASH_KEY:";
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public void testHashPut(String key, String fieldKey, String fieldValue) {
        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(HASH_KEY + key);
        Emp emp = new Emp();
        emp.setEmpNo(fieldKey);
        emp.setEmpName(fieldValue);
        hashOps.put(fieldKey, JSON.toJSONString(emp));
    }

    public String testHashGet(String key, String fieldKey) {
        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(HASH_KEY + key);
        Set<Object> keys = hashOps.keys();
        List<Object> values = hashOps.values();
        String s = (String) hashOps.get(fieldKey);
        return s;
    }
}
